<template>
<div>
  <el-form :model="reqBody" label-position='left'>
    <el-row>
      <el-col :span="8">
        <el-form-item label="用户昵称" property="userName">
          <el-input v-model="userBody.userName">  </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户密码" property="passWord">
          <el-input v-model="userBody.passWord"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="用户角色" property="userRole">
          <el-select v-model="userBody.userRole" placeholder="请选择">
            <el-option
                v-for="(v,index) in this.$store.state.user.roles"
                :key="v"
                :label="v"
                :value="index">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

  </el-form>
  <el-button type="success" v-if="!this.update" @click="addUser">提交</el-button>
  <el-button type="primary" v-else @click="updateUser">保存</el-button>
  <el-button stype="danger" @click="cancel">取消</el-button>
</div>
</template>

<script>

import {userAddRequestBody} from "@/core/request/model/user/UserAddRequestBody";
import {userApi} from "@/core/request/api/UserApi";
import SafeUtils from "@/core/utils/SafeUtils";
import CloneUtil from "@/core/utils/CloneUtil";

export default {
  name: "userMessageBox",
  data(){
    return{
      update:this.$parent.$parent.$parent.select!=null,
      userBody:this.$parent.$parent.$parent.select||new userAddRequestBody(),
      reqBody:null
    }
  },
  methods:{
    async updateUser() {
      if (this.userBody.passWord == null) delete this.userBody.passWord
      this.reqBody = CloneUtil.clone(this.userBody)
      this.reqBody.userGender=this.userBody.userGender
      if (this.userBody.passWord != null) this.reqBody.passWord = SafeUtils.AesEncrypt(this.userBody.passWord, this.$store.state.user.uuid)
      // console.log(this.reqBody);
      await this.axios.post(userApi.userUpdateAdmin, this.reqBody)
          .then(res => {
            if (!res.data.code) {
              this.$message.success(res.data.message)
            }
          })

    },
    cancel(){
      if(this.update){
        this.$parent.$parent.$parent.select=null
        this.$parent.$parent.$parent.edit=false
      }
      else this.$parent.$parent.$parent.addUser=false
    },
    async addUser() {
      if (this.userBody.passWord == null) delete this.userBody.passWord
      this.reqBody = CloneUtil.clone(this.userBody)
      if (this.userBody.passWord != null) this.reqBody.passWord = SafeUtils.AesEncrypt(this.userBody.passWord, this.$store.state.user.uuid)
      // console.log(this.reqBody);
      await this.axios.post(userApi.userAdd, this.reqBody)
          .then(res => {
            if (!res.data.code) {

              this.$message.success(res.data.message)
            }
          })

    }
  },
  mounted() {
    this.$store.dispatch('getUUIDputState');
    this.userBody.passWord=null
  }
}
</script>

<style scoped>

</style>